package com.atguigu.lease.common.sms;

import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.teaopenapi.models.Config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableConfigurationProperties({AliyunSMSProperties.class})
@ConditionalOnProperty(name = "aliyun.sms.endpoint")
public class AliyunSMSConfiguration {

	@Autowired
	private AliyunSMSProperties aliyunSMSProperties;

	@Bean
	public Client createClient() {
		// 创建配置类
		Config config = new Config();
		// 从yml中设置配置属性
		// AccessKey ID
		config.setAccessKeyId(aliyunSMSProperties.getAccessKeyId());
		// AccessKey Secret
		config.setAccessKeySecret(aliyunSMSProperties.getAccessKeySecret());
		// 访问的域名
		config.setEndpoint(aliyunSMSProperties.getEndpoint());

		try {
			// 实例化一个客户端，从 com.aliyun.dysmsapi20170525.Client 类生成对象 client
			return new Client(config);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

}
